//
//  Main2ViewController.swift
//  Hermes
//
//  Created by FH on 2021/5/19.
//  Copyright © 2021 Agora. All rights reserved.
//

import Foundation

// inner instruction's action name list
let ActionNameJoinChannel = "__joinChannel"
let ActionNameLeaveChannel = "__leaveChannel"
let ActionNameEnumCaptureSources = "__enumCaptureSources"
let ActionNameStartCaptureSharing = "__startCaptureSharing"
let ActionNameStopCaptureSharing = "__stopCaptureSharing"
let ActionNameUpdateCaptureParams = "__updateCaptureParams"

// Main VC

class Main2ViewController : FLViewController {
    let appContext = HermesContext()
    let panelStates = NSView()
    let panelGridView = NSView()
    let panelConsole = NSView()
    var tabConsole: FLTabContainer!
    var statesVC: MainStatesPanelVC!
    var gridVC: MainGridStreamsPanelVC!
    var scriptVC: MainScriptPanelVC!
    var loggerVC: MainLoggerPanelVC!
    var capturePanel: ActionScreenCaptureVC?
    
    override func setWindowStyle(currWindow: NSWindow) {
        currWindow.styleMask.remove(.fullSizeContentView)
        currWindow.styleMask.remove(.resizable)
        let linkProjectInfo = self.appContext.isLinkedProject ? ", linkedProject: \(self.appContext.linkedSDKVersion)" : ""
        currWindow.title = "Hermes Mac(appTag: \(self.appContext.appTag), sdkVerion: \(AgoraRtcEngineKit.getSdkVersion())\(linkProjectInfo))"
        currWindow.isMovableByWindowBackground = true
        let size = self.view.bounds.size
        currWindow.setContentSize(CGSize(width: size.width, height: size.height))
        currWindow.center()
        self.dataBinding()
    }
    
    override func setViewStyle() {
        self.view.frame = CGRect(x: 0, y: 0, width: 1440, height: 792)
    }
    
    private func dataBinding() {
        self.appContext.mainStates.$showScreenCaptureTrigger.signal().subscribeNext {[weak self] (showPanel, state) in
            if let self = self {
                if let state = state {
                    if state {
                        // stop capture
                    } else {
                        // show updateParams
                        let panel = self.capturePanel ?? ActionScreenCaptureVC()
                        self.presentAsModalWindow(panel)
                    }
                } else {
                    if showPanel {
                        // prepare to capture
                        let panel = ActionScreenCaptureVC()
                        self.presentAsModalWindow(panel)
                        self.capturePanel = panel
                        self.capturePanel?.rac_appearState.subscribeNext{[weak self] appear in
                            if let self = self, !appear && !self.appContext.mainStates.captureSharing, let panel = self.capturePanel {
                                self.dismiss(panel)
                                self.capturePanel = nil
                            }
                        }
                    } else {
                        // close panel
                        self.capturePanel = nil
                    }
                }
            }
        }
    }
    
    override func layoutUI(needsLayout: Bool, args: [String : Any?]? = nil) {
        // panel states
        self.panelStates.bind(to: yoga, self.view) { it in
            it.minHeight = 60
        }
        if self.statesVC == nil {
            self.statesVC = MainStatesPanelVC(yoga)
        }
        self.statesVC.addTo(superView: self.panelStates)
        // panel grid
        self.panelGridView.bind(to: yoga, self.view) { it in
            it.flexGrow = 1
        }
        if self.gridVC == nil {
            self.gridVC = MainGridStreamsPanelVC(yoga)
        }
        self.gridVC.addTo(superView: self.panelGridView)
        // panel console
        self.panelConsole.bind(to: yoga, self.view) { it in
            it.height = 130
        }
        if self.scriptVC == nil && self.loggerVC == nil {
            self.scriptVC = MainScriptPanelVC(yoga)
            self.loggerVC = MainLoggerPanelVC(yoga)
            self.scriptVC.rac_tabLogger.subscribeNext {[weak self] _ in
                if let self = self {
                    self.tabConsole.nextTab(index: 1)
                }
            }
            self.loggerVC.rac_tabScript.subscribeNext {[weak self] _ in
                if let self = self {
                    self.tabConsole.nextTab(index: 0)
                }
            }
            self.tabConsole = FLTabContainer(yoga, vcList: [self.scriptVC, self.loggerVC])
        }
        self.tabConsole.bind(to: yoga, self.panelConsole) { it in
            it.marginBottom = 4
            it.flexGrow = 1
        }
        
        yoga.apply()
    }
}
